package goods_service

import (
	"fmt"

	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/EDDYCJY/go-gin-example/pkg/alibaba"
	"github.com/shopspring/decimal"
)

// Login 定义登录请求结构体
type AddGoods struct {
	GoodsCommonID int
	GoodsID       int
}

type ZtyGoodsMsg struct {
	ResponseMsg    string `json:"ResponseMsg"`
	ResponseStatus int    `json:"ResponseStatus"`
	Datas          []struct {
		CreateTime    string  `json:"create_time"`
		GoodsClass    string  `json:"goods_class"`
		GoodsCommonID int     `json:"goods_common_id"`
		GoodsID       int     `json:"goods_id"`
		GoodsName     string  `json:"goods_name"`
		GoodsPrice    float64 `json:"goods_price"`
		GoodsStorage  int     `json:"goods_storage"`
		GoodsTransV   float64 `json:"goods_trans_v"`
		Images        string  `json:"images"`
		Spec1         string  `json:"spec1"`
		Spec2         string  `json:"spec2"`
		StoreName     string  `json:"store_name"`
	} `json:"datas"`
}

// AddGoods 执行用户登录逻辑
func (t *AddGoods) AddGoods() (map[string]interface{}, error) {
	var params = map[string]string{}
	params["imageUrl"] = "https://osss.fangzhifu.com/images/840/2025082810533540782.jpeg"
	params["catId"] = "100115"
	params["access_token"] = "2a3b4d6b-3d2e-4a5e-a419-243994dfb551"
	res, _ := alibaba.GetAlibabaAPI("com.alibaba.image:image.product.intelligent.generate", params)
	fmt.Printf("%v\n", res)

	return nil, nil
}

type GetGoods struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetGoods) GetGoods() (interface{}, error) {
	maps := map[string]interface{}{
		"institution_id": t.InstitutionID,
		"is_delete":      0,
		"streamer_id":    0,
	}
	total, _ := models.GetGoodsTotal(maps)
	goodsList, _ := models.GetGoodsPage([]string{"goods_id", "goods_name", "goods_spec", "goods_no", "selling_price", "goods_img", "create_time", "goods_num"}, maps, t.PageNum, t.PageSize, "create_time desc")
	var lists []interface{}

	for _, v := range goodsList {
		sellingPrice := decimal.NewFromInt(int64(v.SellingPrice)).Div(decimal.NewFromInt(100))
		var goods_tag []interface{}
		for _, v1 := range v.GoodsTag {
			var tag = map[string]interface{}{
				"tag_id":   v1.TagID,
				"tag_name": v1.Tag.TagName,
			}
			goods_tag = append(goods_tag, tag)
		}
		params := map[string]interface{}{
			"goods_id":      v.GoodsID,
			"goods_name":    v.GoodsName,
			"goods_spec":    v.GoodsSpec,
			"goods_no":      v.GoodsNo,
			"selling_price": sellingPrice,
			"create_time":   v.CreateTime.Format("2006-01-02 15:04:05"),
			"goods_img":     v.GoodsImg,
			"goods_num":     v.GoodsNum,
			"goods_tag":     goods_tag,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}
